package com.sw.bbs.board.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sw.bbs.board.dao.PostCommentMapper;
import com.sw.bbs.board.dao.user.UserCommentMapper;
import com.sw.bbs.board.dao.user.UserCommentOperationMapper;
import com.sw.bbs.board.pojo.user.UserComment;
import com.sw.bbs.board.pojo.user.UserCommentOperation;
import com.sw.bbs.common.common.ErrorCode;
import com.sw.bbs.common.common.ServiceException;
import com.sw.bbs.board.dao.CommentMapper;
import com.sw.bbs.board.pojo.*;
import com.sw.bbs.board.service.CommentService;
import com.sw.bbs.board.vo.request.comment.InsertCommentRequest;
import com.sw.bbs.board.vo.request.comment.QueryCommentRequest;
import com.sw.bbs.board.vo.request.comment.UpdateCommentRequest;
import com.sw.bbs.user.pojo.User;
import com.sw.bbs.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private PostCommentMapper postCommentMapper;

    @Autowired
    private UserCommentMapper userCommentMapper;

    @Autowired
    private UserCommentOperationMapper userCommentOperationMapper;

    @Autowired
    private UserService authService;

    @Override
    @Transactional(rollbackFor = {ServiceException.class})
    public int insert(InsertCommentRequest record, HttpServletRequest httpServletRequest) throws ServiceException {
        try {
            User user = authService.getUserByRequest(httpServletRequest);
            if (user != null) {
                Integer userId = user.getUserId();
                Comment comment = new Comment();
                BeanUtils.copyProperties(record,comment);
                commentMapper.insertSelective(comment);
                PostComment postComment = new PostComment();
                postComment.setPostId(record.getPostId());
                postComment.setCommentId(comment.getCommentId());

                postCommentMapper.insertSelective(postComment);

                UserComment userComment = new UserComment();
                userComment.setUserId(userId);
                userComment.setCommentId(comment.getCommentId());
                return userCommentMapper.insertSelective(userComment);
            }
            return 0;

        }catch (Exception e) {
            log.error("insert fail");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"insert fail");

        }


    }

    @Override
    public int edit(UpdateCommentRequest record, HttpServletRequest httpServletRequest) throws Exception {
        try {
            Comment comment = new Comment();
            BeanUtils.copyProperties(record,comment);
            return commentMapper.updateByPrimaryKey(comment);
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"update fail");
        }
    }

    @Override
    public int delete(Integer commentId, HttpServletRequest httpServletRequest) throws Exception {
        try {
           return commentMapper.deleteByPrimaryKey(commentId);
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"delete fail");
        }
    }

    @Override
    public PageInfo queryCommentList(QueryCommentRequest request) throws Exception {

        PageHelper.startPage(request.getOffset(),request.getLimit());
        List<Comment> comments = commentMapper.queryCommentByRequest(request);
        return PageInfo.of(comments);
    }


}
